package org.chipu.jnids.sancp;

import java.io.File;
import org.chipu.jnids.Jnids;
import org.chipu.jnids.beans.Profile;
import org.chipu.shared.util.file.FileUtils;
import org.chipu.shared.util.singletons.LoggingUtils;
import org.junit.BeforeClass;
import org.junit.Test;

public class ProfileTester {
	private static Jnids jnids;

	@BeforeClass
	public static void setUp() throws Exception {
		Jnids.sancpDataDir = new File("data/").getAbsolutePath();
		Jnids.sancpConfFile = ProfileTester.class.getResource("/org/chipu/jnids/sancp/sancp.conf").toURI()
			.toString().substring("file:".length());
		jnids = new Jnids();
		jnids.getDbController().cleanProfile();
		// Jnids.getLogger().addHandler(new FileHandler());
	}

	@Test
	public void createProfileFiles() throws Exception {
		long t = System.currentTimeMillis();
		Profile p = new ProfileCreatorFiles(jnids.getDbController(), jnids.getSancpManager(), LoggingUtils
			.getLogger()).createProfile("testProfile", true);
		File outputFile = new File("profile_files.txt");
		FileUtils.createTextFile(outputFile, p.toString());
		FileUtils.createTextFile(new File("encoded_profile_files.xml"), p.encode());
		System.out.println(p);
		System.err.println("----------------------- " + outputFile.getAbsolutePath() + " in "
			+ (System.currentTimeMillis() - t) + " ms");
	}

	// @Test
	public void createProfileDb() throws Exception {
		// 1h: storing flows in db
		// : creating profile
		long t = System.currentTimeMillis();
		ProfileCreatorDb profileCreator = new ProfileCreatorDb(jnids.getDbController(), jnids
			.getSancpManager());
		profileCreator.setLogger(LoggingUtils.getLogger());
		Profile p = profileCreator.createProfile("testProfile", true);
		File outputFile = new File("profile_db.txt");
		FileUtils.createTextFile(outputFile, p.toString());
		System.out.println(p);
		System.err.println("----------------------- " + outputFile + " in "
			+ (System.currentTimeMillis() - t) + " ms");
	}

	// @Test
	// public void decodeEncode() throws Exception {
	// Profile p = Profile.decode(FileUtils.readTextFile(new File("encoded_profile_files.xml")));
	// FileUtils.createTextFile(new File("encoded_profile_files2.xml"), p.encode());
	// }
}